package per.com.controller.portal;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import per.com.bean.User;
import per.com.common.Const;
import per.com.common.ServerResponse;
import per.com.service.PcUsersService;

import javax.servlet.http.HttpSession;

/**
 * @author zxy
 */
@Controller
@RequestMapping("/PcUsers")
public class PcUsersController {

    @Autowired
    @Qualifier("PcUsersServiceId")
    private PcUsersService pcUsersService;

    /**
     * 普通用户登录
     * @param username
     * @param password
     * @param session
     * @return
     */
    @RequestMapping(value = "/login" , method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<User> login(String username , String password , HttpSession session){

        ServerResponse<User> response = pcUsersService.login(username , password);
        if (response.isSuccess()){
            session.setAttribute(Const.CURRENT_USER , response.getData());
        }

        return response;
    }


    /**
     * 普通用户注册
     * @param user
     * @return
     */
    @RequestMapping(value = "/register" , method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse register(User user){

        return pcUsersService.register(user);
    }

    /**
     * 普通用户退出登录
     * @param session
     * @return
     */
    @RequestMapping(value = "/logout" , method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse logout(HttpSession session){
        session.removeAttribute(Const.CURRENT_USER);
        return ServerResponse.createBySuccessMessage("退出登录成功");
    }

    /**
     * 检查用户名是否有效
     * @param str 用户实际得名称
     * @param type 用户名在 bean 实体类中得字段名为 username
     * @return
     */
    @RequestMapping(value = "check_valid" , method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse checkValid(String str , String type){

        return pcUsersService.checkValid(str, type);
    }

    /**
     * 获取用户个人信息
     * @param session
     * @return
     */
    @RequestMapping(value = "get_user_info" , method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<User> getUserInfo(HttpSession session){

        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user != null){
            return ServerResponse.createBySuccess(user);
        }
        return ServerResponse.createByErrorMessage("用户未登录，请先登录");
    }

    /**
     * 得到密保
     * @param username
     * @return
     */
    @RequestMapping(value = "get_question" , method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse getQuestion(String username){

        return pcUsersService.getQuestion(username);
    }

    /**
     * 检查回答密保得答案是否正确
     * @param username
     * @param question
     * @param answer
     * @return
     */
    @RequestMapping(value = "forget_check_answer" , method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse forgetCheckAnswer(String username , String question , String answer){

        return pcUsersService.checkAnswer(username , question , answer);
    }

    /**
     * 忘记密码过程中得重置密码
     * @param username
     * @param passwordNew
     * @param forgetToken
     * @return
     */
    @RequestMapping(value = "forget_rest_password" , method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse forgetRestPassword(String username , String passwordNew , String forgetToken){

        return pcUsersService.forgetRestPassword(username , passwordNew , forgetToken);
    }

    /**
     * 登录状态下得修改密码
     * @param passwordOld
     * @param passwordNew
     * @param session
     * @return
     */
    @RequestMapping(value = "rest_password" , method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse restPassword(String passwordOld , String passwordNew , HttpSession session){

        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null){
            return ServerResponse.createByErrorMessage("用户未登录，请先登录");
        }
        return pcUsersService.restPassword(passwordOld , passwordNew , user);
    }

    @RequestMapping(value = "update_information" , method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<User> updateInformation(User user , HttpSession session){
        User users = (User) session.getAttribute(Const.CURRENT_USER);
        if (users == null){
            return ServerResponse.createByErrorMessage("用户未登录，请先登录");
        }
        user.setUid(users.getUid());
        user.setUsername(users.getUsername());

        ServerResponse<User> response = pcUsersService.updateInformation(user);
        if (response.isSuccess()){
            response.getData().setUsername(users.getUsername());
            session.setAttribute(Const.CURRENT_USER , response.getData());
        }

        return response;

    }
}
